package com.engine4cloud.tdf.supervision.service.impl;

import com.engine4cloud.tdf.common.core.constant.enums.AllAuditActionStatusEnum;
import com.engine4cloud.tdf.common.core.constant.enums.AuditBusinessTypeEnum;
import com.engine4cloud.tdf.common.core.constant.enums.NormalAuditActionStatusEnum;
import com.engine4cloud.tdf.common.core.exception.ValidateCodeException;
import com.engine4cloud.tdf.supervision.dto.SpAuditDTO;
import com.engine4cloud.tdf.supervision.dto.create.SpAuditResultCreateDTO;
import com.engine4cloud.tdf.supervision.dto.result.SpUsualOutResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.SpNormalPreActionUpdateDTO;
import com.engine4cloud.tdf.supervision.service.SpAuditService;
import com.engine4cloud.tdf.supervision.service.SpAuthorizeService;
import com.engine4cloud.tdf.supervision.service.SpUsualOutAuditService;
import com.engine4cloud.tdf.supervision.service.SpUsualOutService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Service
public class SpUsualOutAuditServiceImpl implements SpUsualOutAuditService {

    @Resource
    private SpAuditService auditService;
    @Resource
    private SpAuthorizeService authorizeService;
    @Resource
    private SpUsualOutService usualOutService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean audit(SpAuditDTO dto) {
        SpUsualOutResultDTO old = Optional.ofNullable(usualOutService.getByBatch(dto.getLcbh()))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));
        usualOutService.changeAction(new SpNormalPreActionUpdateDTO(dto.getRybh(), dto.getLcbh(), NormalAuditActionStatusEnum.VILLAGE_AUDIT));
        return auditService.villageAudit(new SpAuditResultCreateDTO(dto, AuditBusinessTypeEnum.USUAL_OUT, AllAuditActionStatusEnum.VILLAGE_AUDIT));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean refuse(SpAuditDTO dto) {
        SpUsualOutResultDTO old = Optional.ofNullable(usualOutService.getByBatch(dto.getLcbh()))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));

        usualOutService.changeAction(new SpNormalPreActionUpdateDTO(dto.getRybh(), dto.getLcbh(), NormalAuditActionStatusEnum.REFUSE));
        return auditService.audit(new SpAuditResultCreateDTO(dto, AuditBusinessTypeEnum.USUAL_OUT, AllAuditActionStatusEnum.REFUSE));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean authorize(SpAuditDTO dto) {
        SpUsualOutResultDTO old = Optional.ofNullable(usualOutService.getByBatch(dto.getLcbh()))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));

        usualOutService.changeAction(new SpNormalPreActionUpdateDTO(dto.getRybh(), dto.getLcbh(), NormalAuditActionStatusEnum.AUTHORIZE));
        return authorizeService.authorize(new SpAuditResultCreateDTO(dto, AuditBusinessTypeEnum.USUAL_OUT, AllAuditActionStatusEnum.AUTHORIZE));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean reject(SpAuditDTO dto) {
        SpUsualOutResultDTO old = Optional.ofNullable(usualOutService.getByBatch(dto.getLcbh()))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));

        usualOutService.changeAction(new SpNormalPreActionUpdateDTO(dto.getRybh(), dto.getLcbh(), NormalAuditActionStatusEnum.REJECT));
        return authorizeService.reject(new SpAuditResultCreateDTO(dto, AuditBusinessTypeEnum.USUAL_OUT, AllAuditActionStatusEnum.REJECT));
    }
}
